#include "stdafx.h"
#include "ZOdbcParameter.h"

namespace	Z	{

namespace	Odbc	{

//------------------------------------------------------------------------------------------------------------------------
Parameter::Parameter(SQLINTEGER kValLen)
:	mValLen(kValLen)
{
}

Parameter::~Parameter()
{
}

bool Parameter::bind(SQLHSTMT hstmt, SQLUSMALLINT ipar, SQLSMALLINT fCType, SQLSMALLINT fSqlType, SQLULEN cbColDef, SQLPOINTER rgbValue, SQLLEN* pcbValue)
{
	const SQLRETURN retcode	=  ::SQLBindParameter(hstmt, ipar, SQL_PARAM_INPUT, fCType, fSqlType, cbColDef, 0, rgbValue, 0, pcbValue);
	return SQL_SUCCEEDED(retcode);
}

//------------------------------------------------------------------------------------------------------------------------
Parameters::Parameters()
{
}

Parameters::~Parameters()
{
	for(ParameterVector::iterator i = mParameters.begin(); i != mParameters.end(); ++i)	{
		(*i)->~Parameter();
	}
}

bool Parameters::bind(SQLHSTMT hstmt)
{
	for(size_t i = 0; i < mParameters.size(); ++i)	{
		if(!mParameters[i]->bind(hstmt, i+1))
			return false;
	}
	return true;
}

SQLRETURN Parameters::putData(SQLHSTMT hstmt)
{
	SQLPOINTER kValue;
	SQLRETURN retcode	= ::SQLParamData(hstmt, &kValue);
	while(retcode == SQL_NEED_DATA)	{
		retcode	= reinterpret_cast<Parameter*>(kValue)->putData(hstmt);
		if(!SQL_SUCCEEDED(retcode))
			return retcode;
		retcode	= ::SQLParamData(hstmt, &kValue);
	}
	return retcode;
}

}

}